package com.kin.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.kin.dao.RoleMapper;
import com.kin.pojo.Page_Info;
import com.kin.pojo.Role;
import com.kin.pojo.User;
import com.kin.service.IPageService;
import com.kin.service.IRoleService;
import com.kin.service.IUserService;

@Service("roleService")
@Transactional
public class RoleService implements IRoleService {
	
	@Resource
	private RoleMapper roleDao;//角色权限Dao
	@Resource
	private IUserService userService;//用户Dao
	@Resource
	private IPageService pageService;//页面资源Dao

	@Override
	public Role getRoleState(int groupid, int pageid) {
		// TODO Auto-generated method stub
		return this.roleDao.selectByGroupAndpage(groupid,pageid);
	}

	@Override
	public Role getRoleById(int roleid) {
		// TODO Auto-generated method stub
		return roleDao.selectByPrimaryKey(roleid);
	}
	
	@Override
	public int updateByGroupidAndPageid(int groupid, int pageid,int state) {
		// TODO Auto-generated method stub
		return roleDao.updateByGroupidAndPageid(groupid, pageid, state);
	}
	
	@Override
	public int getPermission(int userid,String url)
	{
		// TODO Auto-generated method stub
		System.out.println(userid +"=="+url);
		User user = userService.getUserById(userid);
		System.out.println(JSON.toJSONString(user));
		Page_Info page = pageService.getPageByUrl(url);
		System.out.println(JSON.toJSONString(page));
		Role role;
		if(page!= null)
		role = getRoleState(user.getGroupid(), page.getPageid());
		else
		return 2;
		System.out.println(JSON.toJSONString(role));
		return (int)role.getState();
	}
	
	
	

}
